//
//  AWChatMessage.m
//  AtWork2
//
//  Created by lucas on 15/4/16.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWChatMessage.h"

static inline NSString * buildChatDestType(AWChatDestType type) {
    switch (type) {
        case AWChatDestTypeP2P:
            return AWMessageKP2P;
        case AWChatDestTypeDiscussion:
            return AWMessageKDiscussion;
    }
}

static inline NSString * buildChatMessageSenderType(AWChatMessageSenderType type) {
    switch (type) {
        case AWChatMessageSenderFrom:
            return AWMessageKFrom;
        case AWChatMessageSenderTo:
            return AWMessageKTo;
    }
}

static inline NSString * buildBoolString(BOOL result) {
    return result ? @"YES" : @"NO";
}

static inline NSString * buildChatMessageType(AWChatMessageType type) {
    switch (type) {
        case AWChatMessageTypeText:
            return AWMessageKText;
        case AWChatMessageTypeImage:
            return AWMessageKImage;
        case AWChatMessageTypeVoice:
            return AWMessageKVoice;
        case AWChatMessageTypeFile:
            return AWMessageKFile;
        case AWChatMessageTypeSystem:
            return AWMessageKSystem;
        case AWChatMessageTypeArticle:
            return AWMessageKArticle;
        case AWChatMessageTypeEvent:
            return AWMessageKEvent;
    }
}

static inline AWChatDestType parseChatDestType(NSString *type) {
    if ([type.uppercaseString isEqualToString:AWMessageKP2P]) {
        return AWChatDestTypeP2P;
    }
    if ([type.uppercaseString isEqualToString:AWMessageKDiscussion]) {
        return AWChatDestTypeDiscussion;
    }
    return NSNotFound;
}

@implementation AWChatMessage

#pragma mark - Private Methods

- (void)setChatDestType:(AWChatDestType)chatDestType {
    [self bodyWithKey:AWMessageKDestType value:buildChatDestType(chatDestType)];
}

- (AWChatDestType)chatDestType {
    return parseChatDestType([self bodyValueForKey:AWMessageKDestType]);
}

- (void)setChatMessageType:(AWChatMessageType)chatMessageType {
    [self bodyWithKey:AWMessageKType value:buildChatMessageType(chatMessageType)];
}

- (AWChatMessageType)chatMessageType {
    return parseChatMessageType([self bodyValueForKey:AWMessageKType]);
}


- (void)setDisplayName:(NSString *)displayName {
    [self bodyWithKey:AWMessageKDisplayName value:displayName];
}

- (NSString *)displayName {
    return [self bodyValueForKey:AWMessageKDisplayName];
}

- (void)setDisplayAvatar:(NSString *)displayAvatar {
    [self bodyWithKey:AWMessageKDisplayAvatar value:displayAvatar];
}

- (NSString *)displayAvatar {
    return [self bodyValueForKey:AWMessageKDisplayAvatar];
}

// 默认返回@“” 空字符串。
- (NSString *)searchableContent {
    return @"";
}

- (BOOL)isGroup {
    return self.chatDestType == AWChatDestTypeDiscussion;
}

- (NSString *)targetUrl {
    return [self bodyValueForKey:AWMessageKTargetUrl];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@ <%@: %p>",
            [super description],
            self.class,
            self ];
}

- (NSString *)debugDescription {
    return [NSString stringWithFormat:@"%@ <%@: %p %@>",
            [super debugDescription],
            self.class,
            self,
            @{ @"chatMessageType": buildChatMessageType(self.chatMessageType),
               @"chatDestType": buildChatDestType(self.chatDestType),
               @"chatMessageSenderType": buildChatMessageSenderType(self.chatMessageSenderType),
               @"searchableContent": self.searchableContent,
               @"chatMessageSendStatus": buildMessageSendStatus(self.chatMessageSendStatus),
               @"hasRead" : buildBoolString(self.hasRead),
               @"isGroud" : buildBoolString(self.isGroup),
               @"displayName" : self.displayName,
               @"displayAvatar": self.displayAvatar
            }];
}

#pragma mark - Priavte Methods

@end
